<?php ob_start() ?> 
<?php
include_once 'titulo.php';
if ($_SESSION) {
    ?>
    <html>
        <head>
            <title>Comissão Instalador</title>
            <?php
            include_once 'cabecalho.php';
            include_once 'script/conecta.php';
            include_once 'MySQL.php';
            ?>
            <script type="text/javascript" src="script/jquery.maskedinput-1.3.min.js"></script>
            <script>
                jQuery(function($){
                    $("#de").mask("99/99/9999");
                    $("#ate").mask("99/99/9999");
                });
            </script>
            <script>
                $(function() {
                    $.datepicker.setDefaults( $.datepicker.regional[ "pt-BR" ] );
                    $( "#de" ).datepicker();
                    $( "#ate" ).datepicker();
                });	
            </script>
        </head>
        <body>
            <div class="total">
                <div class="cabecalho">
                </div>
                <div class="conteudo">
                    <div class="subconteudo">
                        <?php
                        include_once 'menuHorizontal.php';
                        ?>
                        <?php
                        if (empty($_POST) || $_POST['ACAO'] == "formularioComissao") {
                            formulario_comissao();
                        }
                        ?>

                        <?php
                        if (isset($_POST['ACAO']) && $_POST['ACAO'] == "listaPagamentos") {
                            listaPagamentos($_POST['PESQUISA']);
                        }
                        ?>

                    </div>

                </div>

                <div class="rodape">
                    <?php
                    include_once 'rodape.php';
                    ?>
                </div>
            </div>

            <?php
        } else {
            header("location: session_expirada.php");
        }
        ?>

        <?php

        function formulario_comissao() {
            ?>
            <form action="relatorio_comissaoIns.php" method="POST">
                <table class="tblPesquisar"><tr><td>
                            <label class="label3">Procurar por Nome:</label><br/>
                            <input type="hidden" name="ACAO" value="listaPagamentos" />
                    <tr><td><input type="text" name="PESQUISA" size="50px" class="InputMaior"/></td><td>
                            <input type="submit" value="" class="buttonPesquisar"/>
                </table>
            </form><br/><br/>

            <div style='width:80%; margin-top: 20px; margin-bottom: 20px; margin-left: auto; margin-right: auto;'>
                <form action="relatorio_comissaoIns.php" method="post">
                    <input type="hidden" name="ACAO" value="calcularComissao">
                    <table class="tblLogin">
                        <thead>
                            <tr><td colspan="2"><label>Calcular Comissão</label></tr></td>
                        </thead>
                        <tbody>
                            <tr><td>
                                    <label class="labelMaior">De:</label>
                                <td><input type="text" name="INICIO" id="ate" class="InputMaior">
                            <tr><td><label class="labelMaior">até</label>
                                <td><input type="text" name="FIM" id="de" class="InputMaior">
                                </td></tr>
                            <tr>
                                <td> <label class="labelMaior">Funcionario</label></td>
                                <td>
                                    <?php
                                    $sql = "SELECT *FROM tbl_funcionario";
                                    $resultado = executaSQL($sql);
                                    echo "<select name=FUNCIONARIO class='InputMaior'>";
                                    while ($linha = mysqli_fetch_array($resultado)) {
                                        echo "<option value='" . $linha['fun_id'] . "'>" . $linha['fun_nome'] . "</option>";
                                    }
                                    echo "</select>";
                                    ?>
                                </td>
                            </tr>
                        </tbody>
                        <tfoot>
                            <tr><td colspan="2">
                                    <input type="submit" value="   CALCULAR" class="btnCalcular">
                                </td></tr>
                        </tfoot>
                    </table>
                </form>
            </div>
            <?php
        }
        ?>

        <?php

        function listaPagamentos($pesquisa) {
            echo "<div style='width:98%; margin-top: 20px; margin-bottom: 20px; margin-left: auto; margin-right: auto;'>";

            $sql = "CALL sp_conferirPagamentoByFunc('$pesquisa')";
            $resultado = executaSQL($sql);
            echo "<table class='tblUsuarios'>";
            echo "<caption>Lista de Pagamentos</caption>";
            echo "<th>" . "Funcionario" . "</th>";
            echo "<th>" . "ID" . "</th>";
            echo "<th>" . "Pagador" . "</th>";
            echo "<th>" . "Data" . "</th>";
            echo "<th>" . "De:" . "</th>";
            echo "<th>" . "Até:" . "</th>";
            echo "<th>" . "Salário" . "</th>";
            echo "<th colspan='2'>" . "Comissao" . "</th>";
            echo "<th>" . "Total" . "</th>";
            echo "<th>" . "Ações" . "</th>";

            while ($linha = mysqli_fetch_array($resultado)) {
                echo "<tr>";
                echo "<td>" . $linha["fun_nome"] . "</td>";
                echo "<td>" . $linha["pagf_id"] . "</td>";
                echo "<td>" . $linha["usu_nome"] . "</td>";
                echo "<td>" . $linha["pagf_data"] . "</td>";
                echo "<td>" . $linha["pagf_dt_inicio"] . "</td>";
                echo "<td>" . $linha["pagf_dt_fim"] . "</td>";
                echo "<td>" . $linha["pagf_salario"] . "</td>";
                echo "<td>" . $linha["pagf_comissao"] . "</td>";
                echo "<td>" . $linha["pagf_valor_comissao"] . "</td>";
                echo "<td>" . $linha["pagf_total"] . "</td>";
                echo "<td>";
                echo "<form method='post' action='script/acoes.php'>"; //form de excluir
                echo "<input type='submit' value='' class='btnExcluir'/>"; //botão de excluir
                echo "<input type='hidden'  name='ACAO' value='deletePagamentoFunc'/>";
                echo "<input type='hidden' name='ID' value='" . $linha["pagf_id"] . "'/>";
                echo "</form>";

                echo "</tr>";
            }
            echo "</table>";
        }
        ?>


        <?php

        function executaSQL($sql) {
            $mySQL = new MySQL;
            $resultado = $mySQL->execSP($sql);
            return $resultado;
        }
        ?>

        <?php
        if (!empty($_POST) AND $_POST['INICIO'] != "" AND $_POST['FIM'] != "" AND $_POST['FUNCIONARIO'] != "") {
            $_SESSION['INICIO'] = $_POST['INICIO'];
            $_SESSION['FIM'] = $_POST['FIM'];
            $_SESSION['FUNCIONARIO'] = $_POST['FUNCIONARIO'];
            header("location: relatorio_comissaoInsImpre.php");
        }
        ?>
    </body>
</html>
